package com.example.administrator.rxjavademo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;


public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view)
    {
//        Observable.just(2,6,4)
//                .reduce(new BiFunction<Integer, Integer, Integer>()
//                {
//                    @Override
//                    public Integer apply(Integer i1, Integer i2) throws Exception
//                    {
//                        return i1 + i2;
//                    }
//                }).subscribe(new Consumer<Integer>()
//        {
//            @Override
//            public void accept(Integer integer) throws Exception
//            {
//                Log.e("s", "result = " + integer);
//            }
//        });

//        Observable.zip(getSob(), getIob(), new BiFunction<String, Integer, String>()
//        {
//            @Override
//            public String apply(String s, Integer integer) throws Exception
//            {
//                return s + integer;
//            }
//        }).subscribe(new Consumer<String>()
//        {
//            @Override
//            public void accept(String s) throws Exception
//            {
//                Log.e("s", s);
//            }
//        });

//        final long ss = 10;
//
//        Observable.interval(1, TimeUnit.SECONDS)
//                .map(new Function<Long, Long>()
//                {
//                    @Override
//                    public Long apply(Long aLong) throws Exception
//                    {
//                        return aLong;
//                    }
//                }).take(5)
//                .subscribe(new Consumer<Long>()
//                {
//                    @Override
//                    public void accept(Long aLong) throws Exception
//                    {
//                        Log.e("s", aLong + "_");
//                    }
//                });

//        Flowable.interval(1, TimeUnit.MILLISECONDS)
//                .subscribe(new Consumer<Long>()
//                {
//                    @Override
//                    public void accept(Long aLong) throws Exception
//                    {
//                        SystemClock.sleep(1000);
//                        Log.e("s", aLong + "_");
//                    }
//                });
//        startActivity(new Intent(this, F2Activity.class));
    }

    private Observable<String> getSob()
    {
        return Observable.create(new ObservableOnSubscribe<String>()
        {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception
            {
                e.onNext("A");
                e.onNext("B");
                e.onNext("C");
            }
        });
    }

    private Observable<Integer> getIob()
    {
        return Observable.create(new ObservableOnSubscribe<Integer>()
        {
            @Override
            public void subscribe(ObservableEmitter<Integer> e) throws Exception
            {
                e.onNext(1);
                e.onNext(2);
                e.onNext(3);
            }
        });
    }
}
